//
//  RFAlertView.m
//  RobotFamily
//
//  Created by 曾静 on 16/9/20.
//  Copyright © 2016年 zhangxiaoliang. All rights reserved.
//

#import "RFAlertView.h"
#import "UIImage+Resizable.h"

@interface RFAlertView()
//遮罩
@property (nonatomic, strong) UIView *maskView;
//容器
@property (nonatomic, strong) UIView *containerView;


@property (nonatomic, weak)UIButton *closeBtn;

//二维码视图
@property(nonatomic,strong)UIImageView *qrImageView;

@property(nonatomic,copy)BlockClick block;
@end

@implementation RFAlertView

//创建单例
+ (instancetype)sharedAlertView {
    static RFAlertView *_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[RFAlertView alloc] init];
    });
    return _instance;
}


- (void)showAlertViewWithQRStr:(NSString *)qrStr andBlock:(BlockClick)block{
    _block = block;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    self.maskView = [[UIView alloc] initWithFrame:window.bounds];
    self.maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.50];
    [window addSubview:self.maskView];
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    
    contentView.layer.cornerRadius = 5;
    contentView.layer.masksToBounds = YES;
    
    [window addSubview:contentView];
    self.containerView = contentView;
    
    


    UIButton *closeBtn = [[UIButton alloc]init];
    closeBtn.backgroundColor = [UIColor clearColor];
    self.closeBtn = closeBtn;
    [closeBtn addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];
    [closeBtn setImage:[UIImage imageNamed:@"圆角矩形-9-拷贝"] forState:UIControlStateNormal];
    [window addSubview:closeBtn];
    
    
    UIImageView *qrImageView = [[UIImageView alloc]init];
    [contentView addSubview:qrImageView];
    // 1.创建过滤器，这里的@"CIQRCodeGenerator"是固定的
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    // 2.恢复默认设置
    [filter setDefaults];
    
    // 3. 给过滤器添加数据
    NSString *encodedValue = [qrStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet  URLQueryAllowedCharacterSet]];
    NSData *data = [encodedValue dataUsingEncoding:NSUTF8StringEncoding];
    // 注意，这里的value必须是NSData类型
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    // 4. 生成二维码
    CIImage *outputImage = [filter outputImage];
    
    
    // 5. 显示二维码
    qrImageView.image = [UIImage creatNonInterpolatedUIImageFormCIImage:outputImage withSize:200];
    
    qrImageView.backgroundColor = [UIColor redColor];
    UILabel *label = [[UILabel alloc]init];
    label.text = @"扫一扫，即刻加入";
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:15];
    label.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:label];
    
    
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(250);
        
        make.height.mas_equalTo(250);
        
        make.centerX.equalTo(window.mas_centerX);
        
        make.centerY.equalTo(window.mas_centerY);
        
    }];

    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(contentView.mas_right);
        make.bottom.mas_equalTo(contentView.mas_top);
        make.height.mas_equalTo(35);
        make.width.mas_equalTo(35);
    }];
    
    
    [qrImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(contentView.mas_right).mas_offset(-20);
        make.top.mas_equalTo(contentView.mas_top).mas_offset(20);
        make.left.mas_equalTo(contentView.mas_left).mas_offset(20);
        make.height.mas_equalTo(200);
    }];
    
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(contentView.mas_right).mas_offset(0);
        make.bottom.mas_equalTo(contentView.mas_bottom).mas_offset(-5);
        make.left.mas_equalTo(contentView.mas_left).mas_offset(0);
        make.height.mas_equalTo(20);
    }];
    
}
- (void)closeClick:(UIButton *)sender{

    [self dimiss];
    _block();
}
- (void)dimiss {
    __weak __typeof(&*self)mySelf = self;

    [UIView animateWithDuration:0.25 animations:^{
        self.closeBtn.alpha = 0;
        mySelf.containerView.transform = CGAffineTransformMakeScale(0.6, 0.6);
        mySelf.alpha = 0;
        mySelf.maskView.alpha = 0;
        mySelf.containerView.alpha = 0;
    }
                     completion:^(BOOL finished) {
                         [mySelf.closeBtn removeFromSuperview];
                         [mySelf removeFromSuperview];
                         [mySelf.maskView removeFromSuperview];
                         [mySelf.containerView removeFromSuperview];
                        
                     }];
}

@end
